<template>
  <div>
    <p>当前登录用户为:{{username}}</p>
    <p
      v-for="g in goods"
      :key='g.id'
    >
      {{g.title}}--{{g.price}}-- <img
        :src="g.img"
        alt=""
      > -- <button @click="buy_goods(g.id)">立即购买</button>
    </p>

  </div>
</template>

<script>
export default {
  data() {
    return {
      uid: localStorage.uid,
      username: localStorage.username,
      goods: []  // 商品列表
    }
  },
  created() {
    if (localStorage.uid) { } else {
      location.href = '#/login/'
    }
    this.get_goods()
  },
  methods: {
    get_goods() {
      this.axios.get('http://127.0.0.1:8000/goods/').then(res => {
        console.log(res.data)
        this.goods = res.data
      })
    },
    buy_goods(gid) {
      this.axios.post('http://127.0.0.1:8000/order/', {
        user: this.uid,
        goods: gid,
      }).then(res => {
        alert('下单成功')
        location.href = '#/order/'
      })
    }
  }
}
</script>

<style>
</style>